home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / StateManager / textbump.fx < prev   
Encoding:
Text File  |  2004-09-27  |  5.3 KB  |  157 lines

  1. //--------------------------------------------------------------------------------------
  2. //
  3. // Texture w/Bumpmap Lighting Model
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. //--------------------------------------------------------------------------------------
  7.  
  8. //--------------------------------------------------------------------------------------
  9. // Scene Setup
  10. //--------------------------------------------------------------------------------------
  11.  
  12. // light direction (world space)
  13. float3 lightDir = {0.577, -0.577, 0.577};
  14.  
  15. // light intensity
  16. float4 I_a = { 0.6f, 0.6f, 0.6f, 1.0f };    // ambient
  17. float4 I_d = { 0.5f, 0.5f, 0.5f, 1.0f };    // diffuse
  18. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  19.  
  20.  
  21. // Transformation Matrices
  22. matrix matWorld : WORLD;
  23. matrix matViewProj  : VIEWPROJECTION;
  24. matrix matViewInv   : VIEWINV;
  25.  
  26. texture Texture0 <string name="rock01.jpg";>;
  27. texture Texture1 <string name="rock01.jpg"; bool NormalMap = true;>;
  28.  
  29. //--------------------------------------------------------------------------------------
  30. // Material Properties
  31. //--------------------------------------------------------------------------------------
  32.  
  33. // Set by EffectInstance when mesh is loaded
  34. // (Default values provided for Effect Edit)
  35. float4 Diffuse = float4( 0.95f, 0.95f, 1.f, 1.f );
  36. float4 Ambient = float4( 0.95f, 0.95f, 1.f, 1.f );
  37. float4 Specular = float4( 0.2f, 0.2f,  0.2f, 1.f );
  38.  
  39.  
  40. //--------------------------------------------------------------------------------------
  41. // Vertex Shader
  42. //--------------------------------------------------------------------------------------
  43. void VS( in  float3 pos      : POSITION,
  44.          in  float3 norm     : NORMAL,
  45.          in  float2 iT0      : TEXCOORD0,
  46.          in  float3 Tangent  : TANGENT0,
  47.          out float4 oPos     : POSITION,
  48.          out float2 oT0      : TEXCOORD0,
  49.          out float2 oT1      : TEXCOORD1,
  50.          out float3 oToLight : TEXCOORD2,
  51.          out float3 oHalf    : TEXCOORD3 )
  52. {
  53.     // Transform the vertex to clip space
  54.     float4 Pos_w = mul( float4(pos,1), matWorld );
  55.     oPos = mul( Pos_w, matViewProj );
  56.  
  57.     // Calculate the tangent vectors in world space
  58.     float3 Normal_w = mul( norm, (float3x3)matWorld );
  59.     float3 Tangent_w = mul( Tangent, (float3x3)matWorld );
  60.     float3 Binormal_w = cross( Tangent_w, Normal_w );
  61.  
  62.     // Calculate the tangent matrix
  63.     float3x3 matTSpace = transpose(float3x3( Tangent_w, Binormal_w, Normal_w  ));
  64.  
  65.     // The Eye Position in World space is the last row of the 
  66.     // full-inverse of the view transform
  67.     float3 EyePos_w = matViewInv[3];
  68.     float3 ToEye_w = normalize( EyePos_w - (float3)Pos_w );
  69.     float3 Half_w = normalize( ToEye_w - lightDir );
  70.  
  71.     // Take the light and half-vector into tangent space
  72.     float3 Half_t = mul( Half_w, matTSpace );
  73.     float3 Light_t = mul( -lightDir, matTSpace );
  74.  
  75.     // Pack -1,1 into 0,1
  76.     oHalf = 0.5f * Half_t + 0.5f;
  77.     oToLight = 0.5f * Light_t + 0.5f;
  78.  
  79.     // pass TexCoords through
  80.     oT0 = oT1 = iT0;
  81. }
  82.  
  83. sampler diffuse_sampler = sampler_state
  84. {
  85.     Texture = (Texture0);
  86.     MinFilter = LINEAR;
  87.     MagFilter = LINEAR;
  88.     MipFilter = LINEAR;
  89. };
  90.  
  91. sampler normal_sampler = sampler_state
  92. {
  93.     Texture = (Texture1);
  94.     MinFilter = LINEAR;
  95.     MagFilter = LINEAR;
  96.     MipFilter = LINEAR;
  97. };
  98.  
  99.  
  100. //--------------------------------------------------------------------------------------
  101. // Pixel Shader
  102. //--------------------------------------------------------------------------------------
  103. float4 PS( in  float2 iT0      : TEXCOORD0,
  104.            in  float2 iT1      : TEXCOORD1,
  105.            in  float3 iToLight : TEXCOORD2,
  106.            in  float3 iHalf    : TEXCOORD3 ) : COLOR0
  107. {
  108.     // Sample the diffuse color from the texture
  109.     float3 DiffuseT = tex2D( diffuse_sampler, iT0 );
  110.     
  111.     // Read the normal direction from the texture, unpack from 0,1 to -1,1
  112.     float3 Normal  = tex2D( normal_sampler, iT1 ) * 2 - 1;
  113.  
  114.     // Unpack the Light, and Half-Vector from 0,1 to -1,1
  115.     float3 ToLight = iToLight * 2 - 1;
  116.     float3 Half = iHalf * 2 - 1;
  117.  
  118.     // Calculate the diffuse coefficient
  119.     float NdotL = dot( Normal, ToLight);
  120.  
  121.     // Calcuate the specular coefficient
  122.     float NdotH = dot( Normal, Half );
  123.     NdotH *= NdotH;
  124.     NdotH *= NdotH;
  125.     NdotH *= NdotH;
  126.     NdotH *= NdotH;
  127.  
  128.     // Modulate the Diffuse and Specular colors by the material properties
  129.     float3 DiffuseC = (NdotL * I_d + I_a) * DiffuseT;
  130.     float3 SpecularC = (float3)Specular * NdotH;
  131.     
  132.     // Pass the vertex color through as pixel color
  133.     return float4( DiffuseC + SpecularC, 1.f);
  134. }
  135.  
  136.  
  137. //--------------------------------------------------------------------------------------
  138. // Default Technique
  139. // Establishes Vertex and Pixel Shader
  140. // Ensures base states are set to required values
  141. // (Other techniques within the scene perturb these states)
  142. //--------------------------------------------------------------------------------------
  143. technique tec0
  144. {
  145.     pass p0
  146.     {
  147.         VertexShader = compile vs_1_1 VS();
  148.         PixelShader  = compile ps_1_4 PS();
  149.  
  150.         ZEnable          = TRUE;
  151.         ZWriteEnable     = TRUE;
  152.         AlphaBlendEnable = FALSE;
  153.         CullMode         = CCW;
  154.         AlphaTestEnable  = FALSE;
  155.     }
  156. }
  157.